package com.youlu.campus.monitor.service.impl;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.monitor.entity.WechatMonitor;
import com.youlu.campus.monitor.entity.WechatQuestion;
import com.youlu.campus.monitor.service.WechatMonitorQuestionService;
import com.youlu.campus.monitor.service.WechatMonitorService;
import com.youlu.campus.monitor.service.WechatQuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;

/**
 * 微信监控与问题服务实现类
 *
 * @author su
 * @date 2025/09/02
 */
@Service
@Slf4j
public class WechatMonitorQuestionServiceImpl implements WechatMonitorQuestionService {

    @Autowired
    private WechatQuestionService wechatQuestionService;

    @Autowired
    private WechatMonitorService wechatMonitorService;

    @Override
    public WechatQuestion save(WechatQuestion question) {
        Assert.hasText(question.getMonitorId(), "监控任务id不能为空");
        WechatMonitor monitorById = wechatMonitorService.getWechatMonitorById(question.getMonitorId());
        if (monitorById == null) {
            throw new BusinessException("监控任务不存在");
        }
        wechatQuestionService.save(question);
        //修改关键词
        String keywords = monitorById.getKeywords();
        if (StringUtils.isNotBlank(keywords)) {
            if (!keywords.contains(question.getQuestion())) {
                keywords = keywords + "," + question.getQuestion();
            }
        } else {
            keywords = question.getQuestion();
        }
        monitorById.setKeywords(keywords);
        monitorById.setUpdateTime(LocalDateTime.now());
        wechatMonitorService.saveWechatMonitor(monitorById);
        return question;
    }


}
    